var inquirer = require('inquirer');
var shell = require('shelljs');
var exec = require('child_process').exec;

exports.push = function push(currentBranchName, callback) {
  inquirer
    .prompt([{ type: 'confirm', name: 'push', message: '要push吗？' }])
    .then(answers => answers.push)
    .then(push => {
      if (push === true) {
        const allowPushBarnch = ['develop', 'master', 'alpha'];
        if (allowPushBarnch.indexOf(currentBranchName) < 0) {

          shell.exec('git pull', { silent: true }, function (code, stdout, stderr) {
            if (stdout) {
              exec('git push', function (err) {
                if (!err) {
                  console.log(`------${new Date().toLocaleString()}------`);
                  console.log('------Push成功------');
                  callback && callback();
                }
              });
            } else {
              console.log('git pull 报错，考虑自己手动操作吧', stderr);
            }
          });
        } else {
          console.warn(`${currentBranchName}分支不允许push，如果需要请自行操作。`);
        }
      }
    });
};
